<extend name="base" />

<block name="panel">

  <div class="board">

    <div>
      <table class="table table-bordered">
        <foreach name="news" item="vo" key="id">
            <tr>
              <td><input type="text" value="{$vo.title}" id="title{$vo.news_id}"></td>
              <td>{$vo.update_time}</td>
              <td><button class="news_save btn btn-info" id="{$vo.news_id}">save</button></td>
              <td><button class="display btn btn-info" id="{$vo.news_id}">display</button></td>
              <td><a href="{$baseurl}Console/news/news_id/{$vo.news_id}"><button class="btn btn-info">expand</button></a></td>
              <td><button class="news_delete btn btn-info" id="{$vo.news_id}">delete</button></td>
            </tr>

        </foreach>
      </table>
    </div>

    <div>
      <button id="add" class="btn btn-info"> add </button> <span style="padding-right:10px;"></span>
    </div>
    <br/>

    <div id="add" style="display:none;">
      <table class="table table-bordered">
        <tr>
          <td><input type="text" value="" id="title" placeholder="title" /></td>
          <td><button class="btn btn-info" id="news_add"> save </button></td>
        </tr>
      </table>
      
    </div>

    <br/>
  </div>

  <script type="text/javascript">
  	$(document).ready(function(){

      var baseurl = $("#baseurl").val();
  		
      $.ajax({
        type:"GET",
        url: baseurl+"News/get",
        success: function(data){
          console.log(data['news_en']);
          $("#news_en").html(data['news_en']);
          $("#news_sc").html(data['news_sc']);
          $("#news_tc").html(data['news_tc']);
        }
      })

    $("#news_add").click(function(){
      var title = $("#title").val();

      $.ajax({
        type: "POST",
        url: baseurl+"News/add",
        dataType:"json",
        data:{
          title: title,          
        },
        beforeSend: function(){       
        },
        success: function(data){
          if(data["success"]==1){
            alert("添加成功！");
            window.location.reload();
          }
          else{
            alert(data["error"]);            
          }
        }
      });
    });

    $(".news_delete").click(function(){
      var news_id = $(this).attr('id');
      $.ajax({
        type: "POST",
        url: baseurl+"News/del",
        dataType:"json",
        data:{
          news_id: news_id,          
        },
        beforeSend: function(){       
        },
        success: function(data){
          if(data["success"]==1){
            alert("删除成功！");
            window.location.reload();
          }
          else{
            alert(data["error"]);            
          }
        }
      });

    });


    $(".display").click(function(){
      var news_id = $(this).attr('id');
      $.ajax({
        type: "POST",
        url: baseurl+"News/display",
        dataType: "json",
        data:{
          news_id:news_id,
        },
        success: function(data){
          if(data){
            alert("修改成功");
          }else{
            alert("Failed");
          }
        },
      });
    });



    $(".news_save").click(function(){
			var news_id = $(this).attr('id');
      var title = $("#title"+news_id).val();
      $.ajax({
				type: "POST",
				url: baseurl+"News/editTitle",
				dataType: "json",
				data:{
          title:title,
          news_id:news_id,
				},
				success: function(data){
					if(data){
            alert("修改成功");
          }else{
            alert("Failed");
          }
				},
			});  // ajax finished
		});		

  	});
    
  </script>

</block>


